package com.cop.consultapp.activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.cop.consultapp.MainActivity;
import com.cop.consultapp.adapter.ConsultListAdapter;
import com.cop.consultapp.model.QuestionVO;
import com.cop.consultapp.thread.ConsultListThread;
import com.kdn.consaltapp.R;

public class ConsultListActivity extends Activity {

	private ConsultListThread listThread = null;
	private ConsultListHandler listHandler = null;
	private ConsultListActivity activity = null;
	
	private ListView consultListView = null;
	private Button	btnHome  = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_consult_list);	
		
		setViews();
		
		btnHome.setOnClickListener(onBtnHomeClickListener);
		
		
		this.activity = this;
		this.listHandler = new ConsultListHandler();
		
		this.listThread = new ConsultListThread(listHandler);
		this.listThread.start();
	}
	
	private void setViews(){
		
		this.consultListView = (ListView) findViewById(R.id.listView1);
		this.btnHome = (Button) findViewById(R.id.won_home_btn);
		
	}
	
	private OnClickListener onBtnHomeClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			//Intent intent=new Intent(getApplicationContext(), MainActivity.class);
			//startActivity(intent);
			finish();
			
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.consult_list_acitivity, menu);
		return true;
	}
	
	
	
	public class ConsultListHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			//Log.d("mylog", "결과값 도착!!!!!!!!!! by Handler Message...");
			
			ArrayList<QuestionVO> consultList = (ArrayList<QuestionVO>) msg.obj;
			
			if(consultList==null) return;
			
			Iterator<QuestionVO> consultIter = consultList.iterator();
			
			while(consultIter.hasNext())
			{
				QuestionVO questionVO = consultIter.next();
				//Log.d("mylog", questionVO.getTitle());			
				
			}
			
			ConsultListAdapter adapter = new ConsultListAdapter(activity, R.layout.consult_list_item, consultList);
			
			consultListView.setAdapter(adapter);
		}
		
	}
}
